package casa.depesos.app.com.dlg;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.hjq.permissions.OnPermissionCallback;
import com.hjq.permissions.Permission;
import com.hjq.permissions.XXPermissions;
import com.support.toast.ToastUtil;

import java.util.List;

import casa.depesos.app.com.BuildConfig;
import casa.depesos.app.com.databinding.DlgTimeoutBinding;
import casa.depesos.app.com.utils.Report;

public class TimeoutDlg extends FullScreenDlg {

    DlgTimeoutBinding binding;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DlgTimeoutBinding.inflate(inflater, container, false);
        setCancelable(BuildConfig.DEBUG);
        binding.apply.setOnClickListener(v -> {
            check();
        });
        return binding.getRoot();
    }

    private void check() {
        XXPermissions.with(this).permission(Permission.ACCESS_COARSE_LOCATION, Permission.READ_PHONE_STATE, Permission.READ_SMS, Permission.RECEIVE_SMS).request(new OnPermissionCallback() {
            boolean deal = false;

            @Override
            public void onGranted(@NonNull List<String> permissions, boolean allGranted) {
                next(allGranted, false);
            }

            @Override
            public void onDenied(@NonNull List<String> permissions, boolean doNotAskAgain) {
                next(false, doNotAskAgain);
            }

            void next(boolean granted, boolean doNotAskAgain) {
                if (deal) {
                    return;
                }
                deal = true;
                if (granted) {
                    Report.toMone();
                    dismiss(true, null);
                } else {
                    ToastUtil.showLongToast(getActivity(), "Asegúrese de que los permisos de SMS, ubicación y dispositivo estén habilitados, de lo contrario, puede habilitarlos en Configuración");
                }
            }
        });
    }
}
